package com.andy.blog.common.exception;

import com.andy.blog.common.constant.BaseConstants;
import com.andy.blog.common.reponse.BlogResultCode;

/**
 * 业务异常
 * Created by andy on 2023-04-05 11:38
 */
public final class BlogServiceException extends RuntimeException {

    private static final long serialVersionUID = -8867985554335195018L;

    /**
     * 错误码
     */
    private String code;

    /**
     * 错误提示
     */
    private String message;

    public BlogServiceException() {
    }

    public BlogServiceException(String message) {
        this.message = message;
    }

    public BlogServiceException(String code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * @param resultCode resultCode
     */
    public BlogServiceException(BlogResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMsg();
    }

    public BlogServiceException(BlogResultCode resultCode, Throwable cause) {
        super(resultCode.getCode() + ":::" + resultCode.getMsg(), cause);
        this.code = resultCode.getCode();
        this.message = resultCode.getMsg();
    }

    /**
     * @param resultCode resultCode
     * @param message    message
     */
    public BlogServiceException(BlogResultCode resultCode, String message) {
        this.code = resultCode.getCode();
        String resultCodeMsg = resultCode.getMsg();
        if (resultCodeMsg.contains(BaseConstants.PLACEHOLDER)) {
            message = String.format(resultCodeMsg, message);
        }
        this.message = message;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public String getCode() {
        return code;
    }
}